#include "AABB.h"

#include <algorithm>
using namespace std;

AABB::AABB(const Vector3& _corner1, const Vector3& _corner2)
{
	corner1.x = min(_corner1.x, _corner2.x);
	corner1.y = min(_corner1.y, _corner2.y);
	corner1.z = min(_corner1.z, _corner2.z);
	corner2.x = max(_corner1.x, _corner2.x);
	corner2.y = max(_corner1.y, _corner2.y);
	corner2.z = max(_corner1.z, _corner2.z);
	center = (corner1+corner2)/2;
}

bool AABB::intersect(const AABB& bb2) const
{
	const AABB& bb1 = *this;
	Vector3 c;
	// check if at least one of the 8 corners of the other bb is inside
	for(int i = 0; i < 8; ++i) {
		c.x = ((i & 1) == 0)? bb2.corner1.x : bb2.corner2.x;
		c.y = ((i & 2) == 0)? bb2.corner1.y : bb2.corner2.y;
		c.z = ((i & 4) == 0)? bb2.corner1.z : bb2.corner2.z;
		if(bb1.corner1 < c && c < bb1.corner2)
			return true;
	}
	return false;
}
